/*
 * @Author: “ygnnli” “1585923072@qq.com”
 * @Date: 2024-07-30 09:47:12
 * @LastEditors: “ygnnli” “1585923072@qq.com”
 * @LastEditTime: 2024-08-10 17:12:38
 * @FilePath: /110/30/named_pipe/Comm.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#ifndef __COMM_HPP__
#define __COMM_HPP__

#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
using namespace std;

#define Mode 0666
#define Path "fifo"

class Fifo
{
public:
    Fifo(const string &path)
        : _path(path)
    {
        umask(0);
        int n = mkfifo(_path.c_str(), Mode); //创建命名管道
        if (n == 0)
        {
            cout << "mkfif success" << endl;
        }
        else
        {
            cerr << "mkfif faild,errno" << errno << ",errstring: " << strerror(errno) << endl;
        }
    }

    ~Fifo()
    {
        int n = unlink(_path.c_str());//删除命名管道
        if (n == 0)
        {
            cout << "remove fifo file" << _path << "sucess" << endl;
        }
        else
        {
            cout << "remove failed, errno" << errno << "errstring" << strerror(errno) << endl;
        }
    }

private:
    string _path;
};

#endif